//helper后面需要openid因为自己只能看自己的
const app = getApp()
const ydb = wx.cloud.database()
const _ = ydb.command
Page({
  data: {
    //tab栏的初始选项
    tabIndex: 1,
    //页面需要数据
    //显示的数据
    showList: [],
    //全部预约
    allList: [],
    //待审批
    needToApproveList: [],
    // 已完成
    doneList: [],
    isInit: true,
    //查找结果为空时展示
    isNull: false
  },
  //更改导航栏
  tabFun(e) {
    var that = this
    this.page0 = 0
    this.page1 = 0
    this.page2 = 0
    that.setData({
      showList: [],
      isNull: false
    })
    wx.showToast({
      title: '载入数据中',
      icon: 'loading',
      duration: 1000
    })
    that.setData({
      tabIndex: e.currentTarget.dataset.index,
      isInit: true
    })
    that.getList()
  },
  changeShowList() {
    var that = this
    if (that.data.tabIndex == 0) {
      if (that.data.allList.length <= 0) {
        that.setData({
          isNull: true,
          showList: [],
        })
      } else {
        that.setData({
          showList: that.data.allList
        })
      }

    } else if (that.data.tabIndex == 1) {
      if (that.data.needToApproveList.length <= 0) {
        that.setData({
          isNull: true,
          showList: [],
        })
      } else {
        that.setData({
          showList: that.data.needToApproveList
        })
      }

    } else {
      if (that.data.doneList.length <= 0) {
        that.setData({
          isNull: true,
          showList: [],
        })
      } else {
        that.setData({
          showList: that.data.doneList
        })
      }


    }
  },
  async onShow() {
    var that = this
    this.page0 = 0
    this.page1 = 0
    this.page2 = 0
    this.setData({
      isInit: true
    })
    await this.getList()
    if (that.data.needToApproveList.length <= 0) {
      that.setData({
        isNull: true,
        tabIndex: 1
      })
    } else { //设置页面的tab栏方位
      this.setData({
        tabIndex: 1,
        isNull: false,
      })
    }

    this.changeShowList()
  },

  //触底时加载下一页
 async onReachBottom() {
    var that = this 
    that.page0 += 1
    that.page1 += 1
    that.page2 += 1
    that.setData({
      isInit: false
    })
   await that.getList(true)
  },
  //获取预约队列
  async getList() {
    var that = this
    const PAGE = 6
    wx.showLoading({
      title: '载入数据中',
    })
    //更新全部队列
    if (that.data.tabIndex == 0) {
      let res = await ydb.collection('appointment').skip(that.page0 * PAGE).limit(PAGE).where({
        _openid: app.globalData.userInfo._openid
      }).get({})
      console.log(res)
      if (that.data.isInit) {
        that.setData({
          allList: res.data
        })
      } else {
        //下拉刷新，不能直接覆盖而是累加
        that.setData({
          allList: that.data.allList.concat(res.data)
        })
      }
      wx.hideLoading()
    }
    //更新待审批队列
    if (that.data.tabIndex == 1) {
      let res = await ydb.collection('appointment').skip(that.page1 * PAGE).limit(PAGE).where({
        isDone: 1,
        _openid: app.globalData.userInfo._openid
      }).get({})
      if (that.data.isInit) {
        that.setData({
          needToApproveList: res.data
        })
      } else {
        //下拉刷新，不能直接覆盖而是累加
        that.setData({
          needToApproveList: that.data.needToApproveList.concat(res.data)
        })
      }
      wx.hideLoading()
    }
    //已完成
    if (that.data.tabIndex == 2) {
      let res = await ydb.collection('appointment').skip(that.page2 * PAGE).limit(PAGE).where({
        // isDone: 0 || 2 || 3 || 4,
        isDone: _.or(0, 2, 3, 4),
        _openid: app.globalData.userInfo._openid
      }).get({})
      if (that.data.isInit) {
        console.log(res)
        that.setData({
          doneList: res.data
        })
      } else {
        //下拉刷新，不能直接覆盖而是累加
        that.setData({
          doneList: that.data.doneList.concat(res.data)
        })
      }
      wx.hideLoading()
    }
    that.changeShowList()
  },
  
  //去详情页
  toAppoinementDetail(e) {
    var id = e.currentTarget.dataset.record
    wx.navigateTo({
      url: '/pages/appointmentDetail/appointmentDetail?id=' + id,
    })
  },
})